﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ASPNETVN.PORTAL.Components.UI;
using ASPNETVN.PORTAL.Components.Helpers;
using ASPNETVN.PORTAL.Components.Security;
using System.Data;
using ASPNETVN.PORTAL.Modules.GUIDE.Components;
using System.IO;
using ASPNETVN.PORTAL.Components.Share;

namespace ASPNETVN.PORTAL.Modules.GUIDE.Controls
{
    public partial class AddEditItem : AddEditItemControl
    {
        protected override void OnInit(EventArgs e)
        {
            this.ButtonUpdate = btnUpdate;
            this.ButtonDelete = btnDelete;
            this.ButtonCancel = btnCancel;

            this.Update += AddEditItem_Update;
            this.Delete += AddEditItem_Delete;
            base.OnInit(e);
        }

        void AddEditItem_Delete(object sender, EventArgs e)
        {
            try
            {
                if (this.ItemID > 0)
                {
                    Components.Article.DeleteArticle(this.ItemID);
                    this.ReturnToReferrerPage();
                }
            }
            catch
            {
            }
        }

        void AddEditItem_Update(object sender, EventArgs e)
        {
            try
            {
                Components.Article article = new Components.Article();
                if (this.ItemID > 0)
                {
                    article = Components.Article.Load(this.ItemID);
                }

                int categoryID = cbCategory.SelectedValue == string.Empty ? 0 : Convert.ToInt32(cbCategory.SelectedValue);
                int guideID = cbGuider.SelectedValue == string.Empty ? 0 : Convert.ToInt32(cbGuider.SelectedValue);
                article.CategoryID = categoryID;
                article.GuiderID = guideID;
                article.Title = txtTitle.Text;
                article.Body = HttpUtility.HtmlEncode(txtContent.Text);
                article.CreatedBy = PortalSecurity.CurrentUser.Identity.Email;
                if (this.ItemID == 0)
                {
                    article.CreatedDate = DateTime.Now;
                    this.ItemID = article.Insert();
                }
                else
                {
                    article.CreatedDate = DateTime.Parse(lblCreationTime.Text);
                    article.ID = this.ItemID;
                    article.Update();
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            string url = HttpUrlBuilder.BuildShareUrl(this.ModuleID, this.ItemID, "UploadFile");
            Response.Redirect(url);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ASPNETVN.PORTAL.Components.Share.Category.BuildDropDownListControl(ref cbCategory, this.ModuleID);
                cbGuider.DataSource = Components.Guider.SelectAll(this.ModuleID);
                cbGuider.DataBind();

                lblCreationTime.Text = DateTime.Today.ToShortDateString();
                lblCreatedBy.Text = PortalSecurity.CurrentUser.Identity.Email;

                if (this.ItemID > 0)
                {
                    Components.Article article = Components.Article.Load(this.ItemID);
                    if (article != null)
                    {
                        cbCategory.SelectedValue = article.CategoryID.ToString();
                        txtTitle.Text = article.Title;
                        cbGuider.SelectedValue = article.GuiderID.ToString();
                        txtContent.Text = HttpUtility.HtmlDecode(article.Body);
                        lblCreatedBy.Text = article.CreatedBy;
                        lblCreationTime.Text = article.CreatedDate.ToString();
                    }
                }
                else
                {
                    btnDelete.Visible = false;
                }
            }
        }
    }
}